//
//  EeSafeBottom.swift
//  Alamofire
//
//  Created by zhu xietong on 2018/5/1.
//

import Foundation
import Eelay

open class EeBottomBar:JoView{
    public var bottomJust:CGFloat = 0
    
    public static let specV = UIView()
    
    
    public let contentView:UIView = UIView()
    open var contentHeight:CGFloat{
        get{
            return 50
        }
    }
    
    open override var isHidden: Bool{
        set{
            contentView.alpha = newValue ? 0:1
            super.isHidden = newValue
        }
        get{
            return super.isHidden
        }
    }
    
    static var justBottom:CGFloat = 0
    
    open func didSetController(controller:UIViewController?){
        
    }
    
    public weak var controller:UIViewController? = nil
        {
        didSet{
            didSetController(controller: controller)
            let bar = contentView
            
            if let _ = controller
            {
                backgroundColor = ._background
                if #available(iOS 11, *) {
                    self.addSubview(bar)
                    bar.translatesAutoresizingMaskIntoConstraints = false
                    let guide = self.safeAreaLayoutGuide
                    
                    let top = NSLayoutConstraint(item: bar, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 0)
                    var bottom = guide.bottomAnchor.constraint(equalToSystemSpacingBelow: bar.bottomAnchor, multiplier: 1.0)
                    
                    var bottomHeight = MoUIAdapter.safeInset.bottom
//                    print("---=====KKKK|\(bottomHeight)")
                    if bottomHeight > 10{
                        bottomHeight = -bottomHeight/2.0 - 10
                        //                    bottomHeight = -bottomHeight
                        //                    bottomHeight = 0
                    }
                    else{
                        bottomHeight = 0
                    }
                    bottomJust = -bottomHeight
                    
                    bottom =  NSLayoutConstraint(item: bar, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: bottomHeight)
                    //            bottom.priority = UILayoutPriority.init(rawValue: 1000)
                    
                    
                    let left = NSLayoutConstraint(item: bar, attribute: .left, relatedBy: .equal, toItem: self.safeAreaLayoutGuide, attribute: .left, multiplier: 1, constant: 0)
                    let right = NSLayoutConstraint(item: bar, attribute: .right, relatedBy: .equal, toItem: self.safeAreaLayoutGuide, attribute: .right, multiplier: 1, constant: 0)
                    var barHeight:CGFloat = self.contentHeight

                    if self.isHidden
                    {
                         barHeight = 0
                    }
                        
                    let height = NSLayoutConstraint(item: bar, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: barHeight)
                    
                    
//                    print("---=====bottom-KK|\(bottom)")

                    if barHeight > 0{
                        self.addConstraints([top,left,right,height])
                        NSLayoutConstraint.activate([bottom])
                    }
                    else{
                        bottom = NSLayoutConstraint(item: bar, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0)
                        self.addConstraints([top,left,right,height,bottom])
                        
                    }
                    
                    
//                    self.addConstraints([top,left,right,height])
                    return
                }
                
                
            }
            let barHeight:CGFloat = self.contentHeight
            
            self.eelay = [
                [bar,[ee.T.L.B.R],"\(barHeight)"]
            ]
            
        }
    }
    
    open override func willLayoutRules() {
        
    }
    
  

    
}


open class EeHeightBar:JoView{
    open var height:CGFloat {
        get{
            return 44
        }
    }
    
}

public class EeSafeBottom: JoView{
    
    
    var cons = [NSLayoutConstraint]()
    public var bar:EeHeightBar = EeHeightBar()
    {
        didSet{
            oldValue.removeFromSuperview()
            self.removeConstraints(cons)
            bar.setContentConstrainToHighLever()
            
            
            if #available(iOS 11, *) {
                self.addSubview(bar)
                bar.translatesAutoresizingMaskIntoConstraints = false
                let guide = self.safeAreaLayoutGuide
                let top = NSLayoutConstraint(item: bar, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 0)
                var bottom = guide.bottomAnchor.constraint(equalToSystemSpacingBelow: bar.bottomAnchor, multiplier: 1.0)
                
//                var bottomHeight = TP.bottomLayoutGuide
                var bottomHeight = MoUIAdapter.safeInset.bottom
                
            
                
                
                if bottomHeight > 10{
                    bottomHeight = -bottomHeight/2.0 - 4
//                    bottomHeight = -bottomHeight
//                    bottomHeight = 0
                }
                else{
                    bottomHeight = 0
                }
                bottom =  NSLayoutConstraint(item: bar, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: bottomHeight)
                
                
                let left = NSLayoutConstraint(item: bar, attribute: .left, relatedBy: .equal, toItem: self.safeAreaLayoutGuide, attribute: .left, multiplier: 1, constant: 0)
                let right = NSLayoutConstraint(item: bar, attribute: .right, relatedBy: .equal, toItem: self.safeAreaLayoutGuide, attribute: .right, multiplier: 1, constant: 0)
                
                let barHeight = bar.height
                let height = NSLayoutConstraint(item: bar, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: barHeight)
                
                if barHeight > 0{
                    
                    self.addConstraints([top,left,right,height])
                    NSLayoutConstraint.activate([bottom])
                }
                else{
                    bottom = NSLayoutConstraint(item: bar, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0)
                    self.addConstraints([top,left,right,height,bottom])
                    
                }
                
                
                self.addConstraints([top,left,right,height])
                self.cons = [top,bottom,left,right,height]
                
                
            } else {
                let barHeight = bar.height
                
                self.cons = setEeLays(lays: [
                    [bar,[ee.T.L.B.R],"\(barHeight)"]
                ]).1
            }
            
            
            
            self.backgroundColor = bar.backgroundColor
            bar.layoutIfNeeded()
            self.layoutIfNeeded()
            self.superview?.layoutIfNeeded()
        }
    }
}


